from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser

from api.tests.base_test_case import BaseTestCase
from app.models import Category


class CategoryTest(BaseTestCase):
    def setUp(self):
        super(CategoryTest, self).setUp()

        parent_category = Category(name=u'生鲜')
        parent_category.save()
        sub_category = Category(name=u'蔬菜', parent_category=parent_category)
        sub_category.save()

    def test_category_list(self):
        response = self.client.get('/api/category.json')
        self.assertEqual(response.status_code, 200)

    def test_category_detail(self):
        self.assertTrue(self.access_token)
        response = self.client.get('/api/category/1.json', HTTP_ACCESS_TOKEN=f'{self.access_token}')
        parser = JSONParser()
        parser.parse(BytesIO(response.content))
        self.assertEqual(response.status_code, 200)
